package cn.jiyun.controller;

import cn.jiyun.entity.SmsRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dx")
@CrossOrigin
public class SmsController {

    @PostMapping("/send-sms")
    public String sendSms(@RequestBody SmsRequest request) {
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName(request.getSignName())
                    .setTemplateCode(request.getTemplateCode())
                    .setPhoneNumbers(request.getPhoneNumbers())
                    .setTemplateParam(request.getTemplateParam());
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            client.sendSmsWithOptions(sendSmsRequest, runtime);
            return "短信发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "短信发送失败: " + e.getMessage();
        }
    }

    private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId("LTAI5tHNdh27VhN7Y7pD85Mq")
                .setAccessKeySecret("BNpa9JnZH1j9Vxf2uf5EHkSibxy9zU");
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    @PostMapping("/send-sms1")
    public String sendSms1(@RequestBody SmsRequest request) {
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient1();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName(request.getSignName())
                    .setTemplateCode(request.getTemplateCode())
                    .setPhoneNumbers(request.getPhoneNumbers())
                    .setTemplateParam(request.getTemplateParam());
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            client.sendSmsWithOptions(sendSmsRequest, runtime);
            return "短信发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "短信发送失败: " + e.getMessage();
        }
    }


    private com.aliyun.dysmsapi20170525.Client createClient1() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId("LTAI5tF1mw3jn3zuFFW1qLAy")
                .setAccessKeySecret("D71v3vhz6Qy9rUnWs7UIHw5vrBKCQL");
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }



}


